<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0 ? true : false">
        <el-button
          type="primary"
          size="default"
          icon="Plus"
          @click="addSpu"
          :disabled="CategoryStore.c3Id ? false : true"
          v-has="`btn.Spu.add`"
        >
          添加SPU
        </el-button>
        <el-table border style="margin: 10px 0" :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="default"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
                v-has="`btn.Spu.addsku`"
              ></el-button>
              <el-button
                type="primary"
                size="default"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
                v-has="`btn.Spu.update`"
              ></el-button>
              <el-button
                type="primary"
                size="default"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
                v-has="`btn.Spu.skus`"
              ></el-button>
              <el-popconfirm
                :title="`你确定删除${row.spuName}?`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference :title="`你确定要删除${row.spuName}`">
                  <el-button
                    type="primary"
                    size="default"
                    icon="Delete"
                    title="删除SPU"
                    v-has="`btn.Spu.delete`"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu()"
          @size-change="changeSize()"
        />
      </div>
      <SpuForm
        v-show="scene == 1 ? true : false"
        @changeScene="changeScene"
        ref="Spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2 ? true : false"
        @changeScene="changeScene"
        ref="Sku"
      ></SkuForm>
    </el-card>
    <!-- skuList 展示框 -->
    <el-dialog title="SKU列表" v-model="skuListShow">
      <el-table :data="skuList">
        <el-table-column label="SKU名字" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              lt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: 'Spu',
})
import { ref, watch, onBeforeUnmount } from 'vue'
// 引入分类仓库
import useCategoryStore from '@/store/modules/category'
// 引入SPU数据请求接口
import { reqHasspu, reqRemoveSpu, reqSkuList } from '@/api/product/spu/index'
// 引入SPU数据的数据类型定义
import type {
  HasSpuResponseData,
  Records,
  SpuData,
  SkuInfoData,
  SkuData,
} from '@/api/product/spu/type'
// 引入组件
import SkuForm from './SkuForm/index.vue'
import SpuForm from './SpuForm/index.vue'
import { ElMessage } from 'element-plus'
let CategoryStore = useCategoryStore()

// 为顶部分类菜单传入是否可以选择的值
//场景的数据
let scene = ref<number>(0) //0:显示已有SPU  1:添加或者修改已有SPU 2:添加SKU的结构
// 定义分页器数据
// 当前页码
let pageNo = ref<number>(1)
// 每一页展示几条数据
let pageSize = ref<number>(3)
// 总SPU的条数
let total = ref<number>(0)
//存储已有的SPU的数据
let records = ref<Records>([])
// 获取SpuForm组件的实例
let Spu = ref<any>()
// 获取SkuForm组件的实例
let Sku = ref<any>()
// 收集请求到的Sku数组
let skuList = ref<SkuData[]>([])
// 判断当前skuList展示框显示和隐藏
let skuListShow = ref<boolean>(false)

// function 函数
// 查看Spu的所有sku数据
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuList.value = result.data
    // 显示对话框
    skuListShow.value = true
  } else {
    ElMessage({
      type: 'error',
      message: '查询失败了~请稍后再试试~',
    })
  }
}

// deleteSpu删除Spu
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除Spu成功！',
    })
    //获取剩余SPU数据
    pageNo.value = records.value.length > 1 ? pageNo.value : pageNo.value - 1
    getHasSpu()
  } else {
    ElMessage({
      type: 'success',
      message: '删除Spu成功！',
    })
  }
}

// 为Spu添加Sku
const addSku = (row: any) => {
  scene.value = 2
  Sku.value.initSkuData(CategoryStore.c1Id, CategoryStore.c2Id, row)
}

// 获取SpuList
const getHasSpu = async () => {
  let result: HasSpuResponseData = await reqHasspu(
    pageNo.value,
    pageSize.value,
    CategoryStore.c3Id,
  )
  if (result.code == 200) {
    // 将查询到的SPU数据数组进行存储
    records.value = result.data.records
    total.value = result.data.total
  }
}

//分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSpu()
}

//添加新的SPU按钮的回调
const addSpu = () => {
  //切换为场景1:添加与修改已有SPU结构->SpuForm
  scene.value = 1
  //点击添加SPU按钮,调用子组件的方法初始化数据
  Spu.value.initAddSpu(CategoryStore.c3Id)
}

//修改已有的SPU的按钮的回调
const updateSpu = (row: SpuData) => {
  //切换为场景1:添加与修改已有SPU结构->SpuForm
  scene.value = 1
  //调用子组件实例方法获取完整已有的SPU的数据
  Spu.value.initHasSpuData(row)
}

//子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  //子组件Spuform点击取消变为场景0:展示已有的SPU
  scene.value = obj.flag
  // 添加Sku不需要重新获取数据
  if (obj.params == 'kong') return
  if (obj.params == 'update') {
    //更新留在当前页
    getHasSpu()
  } else {
    pageNo.value = 1
    //添加留在第一页
    getHasSpu()
  }
}

// watch 监视数据
watch(
  () => CategoryStore.c3Id,
  () => {
    // 当c3ID发生变化 发送请求当前展示的SPU数据
    records.value = []
    // 判断c3Id 是否纯在
    //保证三级分类得有才能发请求
    if (!CategoryStore.c3Id) return
    getHasSpu()
  },
)

//路由组件销毁前，情况仓库关于分类的数据
onBeforeUnmount(() => {
  CategoryStore.$reset()
})
</script>

<style scoped></style>
